package data_structure.offer;

public class RandomListNode {

    int value;
    RandomListNode next;
    RandomListNode random;

    public RandomListNode(int value) {
        this.value = value;
    }

    public RandomListNode clone(RandomListNode pHead) {

        if (pHead == null) {
            return null;
        }
        RandomListNode head = new RandomListNode(pHead.value);
        RandomListNode temp = head;

        while (pHead.next != null) {
            temp.next = new RandomListNode(pHead.next.value);
            if (pHead.random != null) {
                temp.random = new RandomListNode(pHead.random.value);
            }
            pHead = pHead.next;
            temp = temp.next;
        }

        return head;
    }
}
